#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define OUTPUT_BUFFER_SIZE 4096
#include <stdbool.h>
#define BUFFER_SIZE 1024
char* execute(const char* command) {
    if (command == NULL) {
        return NULL;
    }

    FILE *fp = popen(command, "r");
    if (fp == NULL) {
        perror("popen failed");
        return NULL;
    }

    char *output = (char*)malloc(OUTPUT_BUFFER_SIZE);
    if (output == NULL) {
        perror("malloc failed");
        pclose(fp);
        return NULL;
    }

    memset(output, 0, OUTPUT_BUFFER_SIZE);

    char buffer[256];
    while (fgets(buffer, sizeof(buffer), fp) != NULL) {
        strncat(output, buffer, OUTPUT_BUFFER_SIZE - strlen(output) - 1);
    }

    pclose(fp);

    // 检查输出是否为空
    if (strlen(output) == 0) {
        free(output);
        return " ";
    }

    return output;
}
void error(const char *msg) {
    perror(msg);
    exit(1);
}

int main(int argc, char** argv) {
    int PORT = 4030;
    char* password = "";
    if (argc >= 2){
	for (int i=0;i<argc;i++) {
	    if (strcmp(argv[i], "-p") == 0) {
		PORT = atoi(argv[i+1]);
            }
            if (strcmp(argv[i], "--password") == 0) {
                password = argv[i+1];
		printf("password setd:%s\n", password);
            }
        }
    }
    
    int sockfd, new_sockfd;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_len = sizeof(client_addr);
    char buffer[BUFFER_SIZE];
    char cmd_output[BUFFER_SIZE * 2];  // Larger buffer to hold command output

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        error("socket creation failed");
    }

    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(PORT);

    if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        error("bind failed");
    }

    if (listen(sockfd, 10) < 0) {
        error("listen failed");
    }
    while (1) {
        new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &client_addr_len);
        if (new_sockfd < 0) {
            error("accept failed");
        }

        printf("Connection accepted\n");
        printf("wait to connect...");
        
	bool nopass = true;
	while (nopass) {
	    int bytesReceived = recv(new_sockfd, buffer, BUFFER_SIZE, 0);

            if (bytesReceived <= 0) {
                if (bytesReceived == 0) {
                    printf("Client disconnected\n");
                } else {
                    perror("recv failed");
                }
                break;
            }
            
            buffer[bytesReceived] = '\0';  // Null-terminate the received string
            if (strcmp(buffer, password) == 0) {
	        nopass = false;
                send(new_sockfd, "succ", strlen("succ"), 0);
	    } else {
		send(new_sockfd, "fail", strlen("fail"), 0);
  	    }
        }
        while (1) {
            int bytesReceived = recv(new_sockfd, buffer, BUFFER_SIZE, 0);
            if (bytesReceived <= 0) {
                if (bytesReceived == 0) {
                    printf("Client disconnected\n");
                } else {
                    perror("recv failed");
                }
                break;
            }

            buffer[bytesReceived] = '\0';  // Null-terminate the received string

            if (strcmp(buffer, "exit") == 0) {
                break;
            }
            char* cmd_output = (char*)malloc(4096);
	    cmd_output = execute(buffer);
            // Clear the output buffer before executing the command
             // Send the command output back to the client
            send(new_sockfd, cmd_output, strlen(cmd_output), 0);
        }

        close(new_sockfd);
    }

    close(sockfd);
    return 0;
}

